package org.PropertyManageMentSystem.Servive;
import org.PropertyManageMentSystem.Manager.*;
import org.PropertyManageMentSystem.Data.*;

import java.sql.SQLException;
import java.util.ArrayList;

public class SchedulerService {

    private Scheduler scheduler;

    private MysqlConfig mysqlConfig;
    private Long schedulerId;
    private ScheduleManager ScheduleManager ;
    ActionManager ActionManager;
    private TaskManager TaskManager;
    private OwnerManager OwnerManager;
    private  WorkerManager WorkerManager;

    private ComplaintManager ComplaintManager;
    private ComplaintReplyManager ComplaintReplyManager;
    public SchedulerService(Long schedulerId, MysqlConfig mysqlConfig) throws SQLException, ClassNotFoundException {
        this.mysqlConfig=mysqlConfig;
        this.schedulerId=schedulerId;
       ScheduleManager = new ScheduleManager(mysqlConfig);
        ActionManager= new ActionManager(mysqlConfig);
        ComplaintManager= new ComplaintManager(mysqlConfig);
        OwnerManager = new OwnerManager(mysqlConfig);
        TaskManager = new TaskManager(mysqlConfig);
        WorkerManager=new WorkerManager(mysqlConfig);
        ComplaintReplyManager = new ComplaintReplyManager(mysqlConfig);
    }

    public void createTask(String ownerName, String ownerTel, String ownerAddress,
                    EmergencyType type, String info, String source) throws Exception {
        OwnerManager.addOwner(ownerName,ownerTel,ownerAddress);
        Long ownerid = OwnerManager.findOwnerByName(ownerName);
        TaskManager.addTask(ownerid,type,info,source);


    }
    public ArrayList<Task> getTaskByState(Task.TaskStates taskstate) throws SQLException {

        return TaskManager.getTaskByState(taskstate);
    }

    public Schedule getScheduleByTaskId(Long taskId) throws SQLException {
        ArrayList<Schedule> scheduleArrayList = ScheduleManager.getScheduleByTaskId(taskId);
        for(Schedule schedule:scheduleArrayList){
            if(schedule.getState() == Schedule.ScheduleState.accept){
                return schedule;
            }
        }
        return null;
    }

    public ArrayList<Worker> getWorkersByType(EmergencyType type) throws Exception {
        ArrayList<Worker> workerArrayList = WorkerManager.getWorkersByType(type);
        ArrayList<Worker> res = new ArrayList<>();
        for (Worker worker:workerArrayList){
            if(worker.getState() == Worker.WorkerState.free){
                res.add(worker);
            }
        }
        return res;
    }
    public void schedule(Long taskId, Long workerId) throws SQLException {
        ScheduleManager.addSchedule(workerId,taskId,this.schedulerId);
        TaskManager.changeTaskStateById(taskId, Task.TaskStates.processing);
        WorkerManager.changeWorkerStateById(workerId, Worker.WorkerState.busy);
    }
    public ArrayList<ComplaintReply> getUnfilledReply(){

        return ComplaintReplyManager.getUnfilledReply(ComplaintReply.Role.scheduler,this.schedulerId);
    }
    public Complaint getRelatedComplaint(Long complaintId){

        return ComplaintManager.getComplaintById(complaintId);
    }
    public void fillReply(Long complaintReplyId, String info){
        ComplaintReplyManager.fillComplaintReply(complaintReplyId,info);
    }
    public void  changeEmergencyTypeById(Long taskId,EmergencyType emergencyType){
        TaskManager.changeEmergencyTypeById(taskId,emergencyType);
    }
}
